<template>
	<view class="uni-container">
		<Editor @editordata="editorData" v-show="editShow" style="height: 100%;"/>
		<view class="" v-show="!editShow" >
			<view class="" @click="editShow=!editShow" style="padding-bottom: 20rpx;">
				<text style="color: #0066CC;font-size: 28rpx;">返回上一级</text>
			</view>
			<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast" >
				<uni-forms-item name="category_id" label="分类" required>
					<uni-data-checkbox v-model="formData.category_id" collection="opendb-news-categories"
						field="name as text, _id as value" />
				</uni-forms-item>
				<uni-forms-item name="title" label="标题" required>
					<uni-easyinput placeholder="标题" v-model="formData.title" trim="both" />
				</uni-forms-item>
				<uni-forms-item name="content" label="文章内容" required>
					<uni-easyinput placeholder="文章内容" maxlength="-1" v-model="formData.content" trim="right" @focus="editShow=!editShow"/>
				</uni-forms-item>
				<uni-forms-item name="excerpt" label="摘要">
					<uni-easyinput placeholder="文章摘录" v-model="formData.excerpt" trim="both" />
				</uni-forms-item>
				<uni-forms-item name="article_status" label="文章状态">
					<uni-data-checkbox v-model="formData.article_status"
						:localdata="formOptions.article_status_localdata" />
				</uni-forms-item>
				<uni-forms-item name="comment_status" label="开放评论">
					<uni-data-checkbox v-model="formData.comment_status"
						:localdata="formOptions.comment_status_localdata" />
				</uni-forms-item>
				<uni-forms-item label="封面大图">
					<uni-file-picker 
					    v-model="imageValue" 
					    fileMediatype="image"
						return-type="object"
						:del-icon="false"
						limit="1"
					    mode="list" 
						:list-styles="listStyles"
					    @select="select" 
					    @success="success" 
					/>
					<!-- <uni-easyinput placeholder="缩略图地址" v-model="formData.avatar" trim="both" /> -->
				</uni-forms-item>
				<uni-forms-item name="publish_date" label="发表时间">
					<uni-datetime-picker return-type="timestamp" :value="formData.publish_date" />
				</uni-forms-item>
				<uni-forms-item name="last_modify_date" label="最后修改时间">
					<uni-datetime-picker return-type="timestamp" :value="formData.last_modify_date" />
				</uni-forms-item>

				<view class="uni-button-group">
					<button type="primary" class="uni-button" @click="submit">提交</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import Editor from '@/components/Editor/editor'
	import {
		validator
	} from '../../js_sdk/validator/opendb-news-articles.js';

	const db = uniCloud.database();
	const dbCollectionName = 'opendb-news-articles';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		components:{
			Editor
		},
		data() {
			return {
				editShow:true,
				formData: {
					"category_id": "",
					"title": "",
					"content": "",
					"excerpt": "",
					"article_status": 0,
					"comment_status": null,
					"avatar": "",
					"publish_date": null,
					"last_modify_date": null,
				},
				formOptions: {
					"article_status_localdata": [{
							"value": 0,
							"text": "草稿箱"
						},
						{
							"value": 1,
							"text": "已发布"
						}
					],
					"comment_status_localdata": [{
							"value": 0,
							"text": "关闭"
						},
						{
							"value": 1,
							"text": "开放"
						}
					]
				},
				rules: {
					...getValidator([ "category_id", "title", "content", "excerpt", "article_status",
						"comment_status", "avatar", "publish_date", "last_modify_date",
					])
				},
				imageValue:[],
				listStyles:{
					"borderStyle":{
						"color":"#eee",     // 边框颜色
						"width":"1px",      // 边框宽度
						"style":"solid",    // 边框样式
						"radius":"5px"      // 边框圆角，不支持百分比
					},
					"border":false, // 是否显示边框
					"dividline":true // 是否显示分隔线
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			select(e){
				console.log(e)
			},
			success(e){
				console.log(e)
				console.log(this.imageValue)
				this.formData.avatar=this.imageValue.fileID
				console.log(this.formData.avatar)
			},
			editorData(res){
				console.log(res,'jhsfjdshfd')
				// return
				// #ifndef MP-WEIXIN
				let data=res
				// #endif
				// #ifdef MP-WEIXIN
				let data=res.detail.__args__[0]
				// #endif
				console.log(data)
				if(data.text=='\n'){
					uni.showToast({
						title:'内容不能为空',
						icon:'none'
					})
					return
				}else{
					this.formData.content=data.html
					this.editShow=false
				}
				
			},
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				value.avatar=this.formData.avatar
				console.log(value)
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(value).then((res) => {
					console.log(value)
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateTo({
						url:'/pages/opendb-news-articles/list'
					}), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		display: flex;
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
</style>
